package com.kailing.bootbatch.test;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.filefilter.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.TimeZone;

/**
 * <h3>partitionjob</h3>
 * <p>file util</p>
 *
 * @author : hduong
 * @version : 1.0
 * @date : 2019-12-09 15:04
 **/
public class FileUtil {

    private static final String FILE_PATH_DELETE = "D:/hongdu/file";

    private static final Logger log = LoggerFactory.getLogger(FileUtil.class);

    /**
     * 清理目录中过期的文件
     *
     * @param dirPath 文件路径
     * @param ms      时间 /毫秒
     */
    public static void timeout(String dirPath, long ms) {
        // 该日期之前的文件
        Date pointDate = new Date(System.currentTimeMillis() - ms);
        // 文件过滤条件
        IOFileFilter timeFileFilter = FileFilterUtils.ageFileFilter(pointDate, true);
        IOFileFilter fileFiles = new AndFileFilter(FileFileFilter.FILE, timeFileFilter);
        File directory = new File(dirPath);
        Iterator<File> itFile = FileUtils.iterateFiles(directory, fileFiles, TrueFileFilter.INSTANCE);
        // 删除符合条件的文件
        while (itFile.hasNext()) {
            delete(itFile.next());
        }
    }

    /**
     * 功能描述:
     * <删除当前时间 多少分钟前的所有文件 >
     *
     * @param dirPath
     * @param minutes
     * @return : void
     * @date : 2019/12/9 15:18
     * @author : hduong
     */
    public static void timeout(String dirPath, int minutes) {
        // 该日期之前的文件
        Date pointDate = new Date(System.currentTimeMillis() - getMillisByMinutes(minutes));
        // 文件过滤条件
        //这个实现我想大家应该自己都会写，就是
        //取得当前文件的最后修改时间，然后和用户传近来的时间点做比较，再根据acceptOlder标志位来看看应该accept。
        IOFileFilter timeFileFilter = FileFilterUtils.ageFileFilter(pointDate, true);
        //只是文件 不是文件夹
        IOFileFilter fileFiles = new AndFileFilter(FileFileFilter.FILE, timeFileFilter);
        File directory = new File(dirPath);
        //列出所有的文件 ：
        Iterator<File> itFile = FileUtils.iterateFiles(directory, fileFiles, TrueFileFilter.INSTANCE);
        // 删除符合条件的文件
        while (itFile.hasNext()) {
            delete(itFile.next());
        }
    }

    /**
     * 删除
     *
     * @param file
     */
    public static void delete(File file) {
        boolean result = file.delete();
        if (result) {
            log.info("删除: {} 成功, 最后一次修改时间:{}", file.getAbsolutePath(), getYearMonthDayHourMinuteSecond(file.lastModified()));
        } else {
            log.warn("删除: {} 失败, 最后一次修改时间:{}", file.getAbsolutePath(), getYearMonthDayHourMinuteSecond(file.lastModified()));
        }
    }

    public static void main(String[] args) {
//        timeout(FILE_PATH_DELETE, 0);
//        System.out.println(System.currentTimeMillis());
//        //2019-12-09 15:14:30
////        System.out.println(getYearMonthDayHourMinuteSecond(System.currentTimeMillis()));
//        System.out.println(getMillisByMinutes(10));
//        timeout(FILE_PATH_DELETE, 0);

//        timeout(FILE_PATH_DELETE, 8);
//        System.out.println(getMillisByDays(8));
//        timeout(FILE_PATH_DELETE, getMillisByDays(8));
        getModifiedTime_2();
    }

    public static void getModifiedTime_2() {
        File f = new File("D:\\workspace\\分布式任务\\xxl-job\\xxl-job-executor-samples\\xxl-job-executor-sample-springboot\\src\\main\\resources\\customer.txt");
        Calendar cal = Calendar.getInstance();
        long time = f.lastModified();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
//        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        cal.setTimeInMillis(time);
        System.out.println(getDaysByMillons(System.currentTimeMillis() - time));
        System.out.println("修改时间[2] " + formatter.format(cal.getTime()));
        //输出：修改时间[2]    2009-08-17 10:32:38
    }

    public static boolean isTen(Date date) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String dateStr = formatter.format(date);
        String curStr = formatter.format(new Date());
        //1 ：获取当前日期
        //2 : 减少10天
        //3 ： 计算文件的最后修改时间
        //4 : 两个对比大小 : 修改时间小的都进行删除
        return false;
    }

    /**
     * 将毫秒转换为年月日时分秒
     *
     * @author GaoHuanjie
     */
    public static String getYearMonthDayHourMinuteSecond(long timeMillis) {
        Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT+8"));
        calendar.setTimeInMillis(timeMillis);
        int year = calendar.get(Calendar.YEAR);

        int month = calendar.get(Calendar.MONTH) + 1;
        String mToMonth = null;
        if (String.valueOf(month).length() == 1) {
            mToMonth = "0" + month;
        } else {
            mToMonth = String.valueOf(month);
        }

        int day = calendar.get(Calendar.DAY_OF_MONTH);
        String dToDay = null;
        if (String.valueOf(day).length() == 1) {
            dToDay = "0" + day;
        } else {
            dToDay = String.valueOf(day);
        }

        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        String hToHour = null;
        if (String.valueOf(hour).length() == 1) {
            hToHour = "0" + hour;
        } else {
            hToHour = String.valueOf(hour);
        }

        int minute = calendar.get(Calendar.MINUTE);
        String mToMinute = null;
        if (String.valueOf(minute).length() == 1) {
            mToMinute = "0" + minute;
        } else {
            mToMinute = String.valueOf(minute);
        }

        int second = calendar.get(Calendar.SECOND);
        String sToSecond = null;
        if (String.valueOf(second).length() == 1) {
            sToSecond = "0" + second;
        } else {
            sToSecond = String.valueOf(second);
        }
        return year + "-" + mToMonth + "-" + dToDay + " " + hToHour + ":" + mToMinute + ":" + sToSecond;
    }

    /**
     * 功能描述:
     * <获取 毫秒数 ： 分钟  *  60  *  1000>
     *
     * @param minutes
     * @return : long
     * @date : 2019/12/9 15:17
     * @author : hduong
     */
    public static long getMillisByMinutes(int minutes) {
        return minutes * 60 * 1000;
    }

    /**
     * 功能描述:
     * <获取天的描述>
     *
     * @param days
     * @return : long
     * @date : 2019/12/9 15:25
     * @author : hduong
     */
    public static long getMillisByDays(int days) {
        return getMillisByMinutes(days * 24 * 60);
    }

    public static int getDaysByMillons(long millions) {
        return (int) millions / 1000 /60 /60 /24;
    }

}
